/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.synchronization.worker;

import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.swing.SwingWorker;
import systole.exceptions.ExceptionDAO;
import systole.exceptions.ExceptionWS;
import systole.persistence.FacadeDB;
import systole.synchronization.SyncController;
import systole.synchronization.remote.ws.SystoleSyncWS;
import systole.synchronization.view.SyncEntity;

/**
 *
 * @author Juan Manuel
 */
public class SynchronizationWorker extends SwingWorker<Boolean, SyncEntity> {

    private SyncController controller;
    private SystoleSyncWS systoleSyncWS;
    private boolean errorOnSync = false;

    /**
     * 
     * @param controller 
     */
    public SynchronizationWorker(SyncController controller) {
        this.controller = controller;
        this.systoleSyncWS = new SystoleSyncWS();
    }

    @Override
    protected Boolean doInBackground() throws Exception {
        try {
            this.setProgress(0);
            if (this.isCancelled()) {
                return Boolean.FALSE;
            }
            this.systoleSyncWS.connectToWS();
            if (this.isCancelled()) {
                return Boolean.FALSE;
            }

            errorOnSync = !this.systoleSyncWS.downloadMedicines();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(1);

            errorOnSync = !this.systoleSyncWS.uploadMedicines();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(3);

            errorOnSync = !this.systoleSyncWS.mergeMedicines();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(5);

            errorOnSync = !this.systoleSyncWS.downloadFamily();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(7);

            errorOnSync = !this.systoleSyncWS.uploadFamily();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(9);

            errorOnSync = !this.systoleSyncWS.mergeFamily();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(11);

            errorOnSync = !this.systoleSyncWS.downloadPathologies();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(12);

            errorOnSync = !this.systoleSyncWS.uploadPathologies();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(15);

            errorOnSync = !this.systoleSyncWS.mergePathologies();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(18);

            errorOnSync = !this.systoleSyncWS.downloadProfessions();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(21);

            errorOnSync = !this.systoleSyncWS.uploadProfessions();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(24);

            errorOnSync = !this.systoleSyncWS.mergeProfessions();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(27);

            errorOnSync = !this.systoleSyncWS.downloadSignalFrequency();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(30);

            errorOnSync = !this.systoleSyncWS.uploadSignalFrequency();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(33);

            errorOnSync = !this.systoleSyncWS.mergeSignalFrequency();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(33);

            errorOnSync = !this.systoleSyncWS.downloadSports();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(36);

            errorOnSync = !this.systoleSyncWS.uploadSports();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(39);

            errorOnSync = !this.systoleSyncWS.mergeSports();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(42);

            errorOnSync = !this.systoleSyncWS.downloadSurgeries();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(45);

            errorOnSync = !this.systoleSyncWS.uploadSurgeries();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(48);

            errorOnSync = !this.systoleSyncWS.mergeSurgeries();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(51);

            errorOnSync = !this.systoleSyncWS.downloadIdentityCardType();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(54);

            errorOnSync = !this.systoleSyncWS.uploadIdentityCardType();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(57);

            errorOnSync = !this.systoleSyncWS.mergeIdentityCardType();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(60);

            errorOnSync = !this.systoleSyncWS.downloadMedics();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(63);
            errorOnSync = !this.systoleSyncWS.uploadMedics();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(66);
            errorOnSync = !this.systoleSyncWS.mergeMedics();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }

            this.setProgress(68);
            errorOnSync = !this.systoleSyncWS.downloadPatients();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(70);
            errorOnSync = !this.systoleSyncWS.uploadPatients();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(75);
            errorOnSync = !this.systoleSyncWS.mergePatients();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }

            this.setProgress(80);
            errorOnSync = !this.systoleSyncWS.downloadClinicalInfo();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(85);
            errorOnSync = !this.systoleSyncWS.uploadClinicalInfo();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(90);
            errorOnSync = !this.systoleSyncWS.mergeClinicalInfo();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(95);
            errorOnSync = !this.systoleSyncWS.uploadAnalysis();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            errorOnSync = !this.systoleSyncWS.downloadAverages();
            if ((this.isCancelled()) || (this.errorOnSync)) {
                return Boolean.FALSE;
            }
            this.setProgress(100);
            return Boolean.TRUE;
        } catch (ExceptionDAO dAO) {
            this.errorOnSync = true;
            FacadeDB.getInstance().refreshSession();
            return Boolean.FALSE;
        } catch (ExceptionWS ews) {
            this.errorOnSync = true;
            FacadeDB.getInstance().refreshSession();
            return Boolean.FALSE;
        } catch (Exception ex) {
            this.errorOnSync = true;
            FacadeDB.getInstance().refreshSession();
            return Boolean.FALSE;
        }
    }

    @Override
    protected void done() {

        if (this.isCancelled()) {
            this.controller.syncCanceled();
        }
        try {
            if (this.get()) {
                this.controller.syncFinished();
            } else {
                if (this.errorOnSync) {
                    this.controller.syncWithError();
                } else {
                    this.controller.syncCanceled();
                }
            }
        } catch (InterruptedException ex) {
            this.controller.syncCanceled();
        } catch (ExecutionException ex) {
            this.controller.syncCanceled();
        }
    }

    @Override
    protected void process(List<SyncEntity> chunks) {
    }
}
